import request from '@/utils/request'

/**
 * 获取符合条件的全球企业数量
 * @param {Object} params 查询参数
 * @returns {Promise} 返回Promise对象
 */
export function getGlobalEnterprisesCount(params) {
  return request({
    url: '/enterprises/global/count',
    method: 'post',
    data: params,
    headers: {
      'Content-Type': 'application/json'
    }
  })
}

/**
 * 获取用户余额信息
 * @returns {Promise} 返回Promise对象
 */
export function getUserBalanceInfo() {
  return request({
    url: '/api/balance/info',
    method: 'get'
  })
}

/**
 * 导出全球企业数据
 * @param {Object} params 导出参数
 * @returns {Promise} 返回Promise对象
 */
export function exportGlobalEnterprises(params) {

  // 分离参数，exportCount、selectedFields和language作为URL参数，其他参数放在请求体
  const exportCount = params.exportCount;
  const selectedFields = params.selectedFields;
  const language = params.language;
  const { exportCount: _, selectedFields: __, language: ___, ...bodyParams } = params;

  // 构建URL参数
  let url = `/enterprises/global/export?exportCount=${exportCount}`;
  if (selectedFields && selectedFields.trim() !== '') {
    url += `&selectedFields=${encodeURIComponent(selectedFields)}`;
  }
  if (language && language.trim() !== '') {
    url += `&language=${encodeURIComponent(language)}`;
  }


  return request({
    url: url,
    method: 'post',
    data: bodyParams,
    responseType: 'blob',
    headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json, application/octet-stream, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    },
    meta: {
      isExport: true
    },
    retry: 1,
    retryDelay: 2000,
    timeout: 600000 // 设置10分钟超时
  })
}

/**
 * 从ES中查询城市数据
 * @param {String} keyword 搜索关键词
 * @returns {Promise} 返回Promise对象
 */
export function searchCitiesFromES(keyword) {
  return request({
    url: '/enterprises/global/cities',
    method: 'get',
    params: { keyword }
  })
}

/**
 * 从ES中查询行业数据
 * @param {String} keyword 搜索关键词
 * @returns {Promise} 返回Promise对象
 */
export function searchIndustriesFromES(keyword) {
  return request({
    url: '/enterprises/global/industries',
    method: 'get',
    params: { keyword }
  })
}

/**
 * 从ES中查询部门数据
 * @param {String} keyword 搜索关键词
 * @returns {Promise} 返回Promise对象
 */
export function searchDepartmentsFromES(keyword) {
  return request({
    url: '/enterprises/global/departments',
    method: 'get',
    params: { keyword }
  })
}

/**
 * 从ES中查询职业数据
 * @param {String} keyword 搜索关键词
 * @returns {Promise} 返回Promise对象
 */
export function searchOccupationsFromES(keyword) {
  return request({
    url: '/enterprises/global/occupations',
    method: 'get',
    params: { keyword }
  })
}